<?php
require_once('../classes/nusoap.php');
class SugarCRMWebServices {
    var $username;
    var $password;
    var $session;
    var $soap;
    
    function __construct($username, $password)
    {
        $this->username = $username;
        $this->password = $password;
        $this->soap = new nusoap_client('http://www.murillodigital.com/relations/soap.php?wsdl');
    }
    
    function login()
    {
        $result = $this->soap->call('login', array(
            'user_auth' => array(
                'user_name' => $this->username, 
                'password' => md5($this->password), 
                'version' => '.01'), 
                'application_name' => 'murillodigital.com'));
        $this->session = $result['id'];
    }
    
    function createLead($data)
    {
        $name_value_list = array();
        foreach($data as $key => $value)
            array_push($name_value_list, array(
                'name' => $key, 
                'value' => $value));
        $result = $this->soap->call(
            'set_entry', array(
                'session' => $this->session,
                'module_name' => 'Leads',
                'name_value_list' => $name_value_list
            ));
        return $result;
    }
    function getLeadDetails($lead_id) {
        $result = $this->soap->call(
                'get_entry', array(
                    'session' => $this->session,
                    'module_name' => 'Leads',
                    'id' => $lead_id
                    )
                );
        $result_array = array(
            'first_name' => $result["entry_list"][0]["name_value_list"][12]["value"],
            'last_name' => $result["entry_list"][0]["name_value_list"][13]["value"],
            'date_contact' => date('j-m-Y', strtotime($result["entry_list"][0]["name_value_list"][4]["value"])),
            'current_status' => $result["entry_list"][0]["name_value_list"][40]["value"]
        );
        return $result_array;
        //print_r($result);
    }
    function getLastEmail($lead_id) {
        
    }
}
?>
